<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>修改预计上门时间</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<link rel="stylesheet" href="../../../css/mui.min.css">
		<link rel="stylesheet" href="../../../css/global.css">
		<link rel="stylesheet" href="../../../css/style.css">
		<link href="../../../css/task.css" rel="stylesheet" />
		<style type="text/css">
			input.d_ipt {
				width: auto;
			}

			.inline-block {
				color: #a9a9a9;
			}
		</style>
		<style type="text/css">
			#div {
				width: 0px;
				height: 0px;
				background: red;
				position: fixed;
				top: 65%;
				left: 50%;
			}
			/*移除底部或顶部三角,需要在删除此代码*/
			.mui-popover .mui-popover-arrow:after {
				width: 0px;
			}
			.selectDate{
				background: url(../../../images/icon_32.png) no-repeat right center !important;
				background-size: 14px 13px !important;
				color: #333 !important;
			}
		</style>
	</head>

	<body>
		
		<div id="div"></div>
		<div id="popover" class="mui-popover shadow popover">
			<p class="title" id="title">与您之前的工作计划时间冲突，是否确定？</p>
			<div class="btn_box">
				
				<span class="fl" onclick="closepop()">取消</span>
				<span class="fr red" id="quedingupdate">确定</span>
			</div> 
		</div>
		
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 id="title" class="mui-title">修改预计上门时间</h1>
		</header>
		<div class="mui-content">
			<div class="details_con">
				<div class="clum">
					<span class="name">服务编号</span>
					<span class="txt" id="serviceNo"></span>
				</div>
				<div class="clum">
					<span class="name">服务角色</span>
					<span class="txt">
						<input class="serviceRole" name="serviceRole" type="radio" value="1" disabled="disabled">现场服务&nbsp;&nbsp;
						<input class="serviceRole" name="serviceRole" type="radio" value="2" disabled="disabled">实习
					</span>

				</div>
				<div class="my-content-cell">
					<div class="inline-block" id="pjcgqd">
						<em>查看计划安排</em>
						<span class="mui-icon mui-icon-arrowright arrow-right-position"></span>
					</div>
				</div>
				<div class="clum">
					<span class="name">预计上门时间<em class="t_red">*</em></span>
					<button id='estimatedVisitingTime' type="button" class="selectDate">请选择时间</button>
				</div>
				<div class="clum">
					<span class="name">预计工时<em class="t_red">*</em></span>
					<input id="estimatedWorkingHours" type="number" class="d_ipt" placeholder="请输入预计工时" onchange="jisuan('1')" />
				</div>
				<div class="clum">
					<span class="name">预计离开时间<em class="t_red">*</em></span>
					<!-- <input id="estimatedDepartureTime" type="text" class="d_ipt" placeholder="自动计算" disabled="disabled" /> -->
					<span class="txt" id="estimatedDepartureTime">自动计算</span>
				</div>
			</div>
			<div class="btn_box">
				
				<span class="fl" id="cancel">取消</span>
				<span class="fr red" id="save">保存</span>
			</div>
			<div style="height: 50px;"></div>
		</div>

		<!-- js代码 start -->

		<!-- 基础js start -->
		<script src="../../../js/mui.min.js"></script>
		<script src="../../../js/utils/h.js"></script>
		<script src="../../../js/constant.js"></script>
		<script src="../../../js/common.js"></script>
		<script src="../../../js/utils/base64.js"></script>
		<script src="../../../js/utils/md5.js"></script>
		<script src="../../../js/utils/sortMap.js"></script>
		<script src="../../../js/utils/http.js"></script>
		<script src="../../../js/app.js"></script>
		<script src="../../../js/sqliteTools.js"></script>
		<!-- 基础js end -->
		<script src="../../../js/utils/rolldate.min.js"></script>

		<script type="text/javascript" charset="utf-8">
			var ServiceNoticeId
			var serviceNo
			var planId
			var serviceRole
			var estimatedVisitingTime
			var estimatedWorkingHours
			var estimatedDepartureTime
			//mui初始化
			(function($, doc) {
				$.init();

				// 取消方法
				h("#cancel").tap(function() {
					$.back()
				});

				new Rolldate({
					el: '#estimatedVisitingTime',
					format: 'YYYY-MM-DD hh:mm',
					beginYear: 2015,
					endYear: 2050,
					trigger: 'click',
					lang: {
						title: '请选择预计上门时间',
					},
					confirm: function(date) {
						jisuan(date)
					}
				})

				$.plusReady(function() {
					// 从上一个页面取值
					var self = plus.webview.currentWebview();
					// 获取上一个页面传过来的参数并显示到页面
					ServiceNoticeId = self.ServiceNoticeId;
					serviceNo = self.serviceNo;
					planId = self.planId;
					serviceRole = self.serviceRole;

					console.log("ServiceNoticeId:" + ServiceNoticeId + "	serviceNo:" + serviceNo + "	planId:" + planId)

					h("#serviceNo").html(serviceNo)

					if (serviceRole) {
						// h(".serviceRole").find("input[name='serviceRole'][value='1']").attr('checked','true');
						document.querySelector(`input[name='serviceRole'][value='${serviceRole}']`).checked = true;
					}

					initData()

					// 保存
					h("#save").tap(function() {
						app.showWaiting('正在提交...')
						// 调用计算方法
						jisuan('1');
						estimatedVisitingTime = h("#estimatedVisitingTime").html();
						if (estimatedVisitingTime == null || estimatedVisitingTime == "" || estimatedVisitingTime == "请选择时间") {
							app.closeWaiting()
							app.toast("请选择预计上门时间");
							return
						}
						estimatedWorkingHours = h("#estimatedWorkingHours").val();
						if (estimatedWorkingHours == null || estimatedWorkingHours == "") {
							app.closeWaiting()
							app.toast("请输入预计工时");
							return
						}
						estimatedDepartureTime = h("#estimatedDepartureTime").html();

						var evt = stringYYYYMMDDHHMMToDate(estimatedVisitingTime).getTime();
						var newDate = new Date().getTime();
						if(evt <= newDate){
							app.closeWaiting()
							app.toast("预计上门时间不可以小于当前时间");
							return
						}

						ajaxGet(BASE_URL + "cc/PlanDetails/checkPlanDetailsList", {
							"enginnerId": app.getUser().enginnerId,
							"estimatedVisitingTime": estimatedVisitingTime,
							"estimatedDepartureTime": estimatedDepartureTime,
							"planId": planId
						}, function(data) {
							 //console.log(JSON.stringify(data))
							 if(data.code==200){
								saveIn()
							 }else{
								 app.closeWaiting()
								 mui("#popover").popover('toggle', document.getElementById("div"));
							 }
						}, function(errorMsg) {
							app.closeWaiting()
							app.toast(errorMsg)
							console.log(JSON.stringify(errorMsg))
						});

					});

					// 查看计划安排
					h("#pjcgqd").tap(function() {
						app.jumpTo("/view/service/taskDetails/task_details.html", "view/service/taskDetails/task_details.html", {
							serviceNoticeId: ServiceNoticeId,
							serviceNo: serviceNo,
							enginnerId: app.getUser().enginnerId,
						});
					});

					h("#quedingupdate").tap(function(){
						 app.showWaiting('正在提交...')
						 saveIn()
					})

				});



			}(mui, document));

			// 数据初始化
			function initData() {
				app.showWaiting('加载中...')
				ajaxGet(BASE_URL + "cc/PlanDetails/getDetailsById", {
					"id": planId
				}, function(data) {
					app.closeWaiting()
					console.log(JSON.stringify(data))
					if (data.code == 200) {
						var ets = data.data.estimatedVisitingTime.split(":");
						h("#estimatedVisitingTime").html(ets[0]+":"+ets[1]);
						h("#estimatedWorkingHours").val(data.data.estimatedWorkingHours);
						var eds = data.data.estimatedDepartureTime.split(":");
						h("#estimatedDepartureTime").html(eds[0]+":"+eds[1]);
					}
				}, function(errorMsg) {
					console.log(JSON.stringify(errorMsg))
				});
			}

			// 自动计算方法
			function jisuan(date) {
				var estimatedVisitingTime;
				if (date != "1") {
					estimatedVisitingTime = date
				} else {
					estimatedVisitingTime = h("#estimatedVisitingTime").html();
				}
				if (estimatedVisitingTime == null || estimatedVisitingTime == "" || estimatedVisitingTime == "请选择时间") {
					return
				}
				var evt = stringYYYYMMDDHHMMToDate(estimatedVisitingTime).getTime();
				var estimatedWorkingHours = h("#estimatedWorkingHours").val();
				if (estimatedWorkingHours == null || estimatedWorkingHours == "") {
					return
				}
				var edt = evt + estimatedWorkingHours * 60 * 60 * 1000;
				var estimatedDepartureTime = new Date(edt).Format("yyyy-MM-dd HH:mm");
				h("#estimatedDepartureTime").html(estimatedDepartureTime);
			}
			
			function saveIn(){
				ajaxGet(BASE_URL + "cc/PlanDetails/updateEstimatedVisitingTime", {
					"enginnerId": app.getUser().enginnerId,
					"planId": planId,
					"estimatedVisitingTime": estimatedVisitingTime,
					"estimatedWorkingHours": estimatedWorkingHours,
					"estimatedDepartureTime": estimatedDepartureTime
				}, function(data) {
					app.closeWaiting()
					console.log(JSON.stringify(data))
					if (data.code == 200) {
						app.toast("修改成功");
						mui.back()
					} else {
						app.toast(data.msg);
					}
				}, function(errorMsg) {
					console.log(JSON.stringify(errorMsg))
				});
			}
			
			function closepop(){
				mui("#popover").popover('hide', document.getElementById("div"));
			}
		</script>
		<!-- js代码 end -->

	</body>

</html>
